using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MPSizectorDotNet
{
	public class MPDisplayer3D : UserControl
	{
		public enum MPModelPolygonType
		{
			Point,
			Line,
			Fill
		}

		public enum MPModelTextureType
		{
			GrayImage,
			ColorDepthImage,
			GrayAndColorDepthMixed
		}

		private bool m_Loaded = false;

		private Color4[] m_GrayArr;

		private Color4[] m_ColorArr;

		private Vector3[] m_PointCloud;

		private Color4[] m_PointColor;

		private Vector3[] m_NormCloud;

		private uint[] m_ElementArray;

		private uint[] m_ElementArrayDisplay;

		private int m_ElementDisplayCount = 0;

		private int VboID;

		private int CboID;

		private int NboID;

		private int EboID;

		private float m_XRot = 0f;

		private float m_YRot = 0f;

		private float m_ZRot = 0f;

		private float m_Zoom = 12f;

		private float m_XTrans = 0f;

		private float m_YTrans = 0f;

		private float m_zTrans = 0f;

		private Point m_LastPos;

		private uint m_LastSize = 0u;

		private bool m_IsFirstDisplay = true;

		private float m_DeviceFOV = 40f;

		private float m_DeviceDx = 0.055f;

		private MPModelPolygonType m_ModelPolygon = MPModelPolygonType.Fill;

		private MPModelTextureType m_ModelTexture = MPModelTextureType.GrayAndColorDepthMixed;

		private bool m_UseNormVector = true;

		private float m_ColorDepthImageMin = -10f;

		private float m_ColorDepthImageMax = 10f;

		private MP3DFrameManaged m_Data3DManaged;

		private int m_RotateCnt = 0;

		private float m_RotatePhase;

		private float dx;

		private float dy;

		private float tmpx;

		private float tmpy;

		private IContainer components = null;

		private GLControl mGlControl;

		private Button SettingsExpandBtn;

		private GroupBox Display3DSettingsGbp;

		private Button SetRangeBtn;

		private Label label4;

		private TextBox RangeMaxTbx;

		private Label label3;

		private TextBox RangeMinTbx;

		private Label label2;

		private RadioButton TextureMixedRbt;

		private RadioButton TextureColorDepthRbt;

		private RadioButton TextureGrayRbt;

		private CheckBox UseNormCkb;

		private GroupBox groupBox2;

		private RadioButton PolygonFillRbt;

		private RadioButton PolygonPointRbt;

		private RadioButton PolygonLineRbt;

		private GroupBox groupBox1;

		private GroupBox RotationGbp;

		private HScrollBar RotateAmplitudeScb;

		private HScrollBar RotateSpeedScb;

		private Label label5;

		private Label label1;

		private CheckBox RotateEnableCkb;

		private Timer RotateTmr;

		public bool IsLoaded
		{
			get
			{
				return this.m_Loaded;
			}
		}

		public MPModelPolygonType ModelPolygon
		{
			get
			{
				return this.m_ModelPolygon;
			}
			set
			{
				switch (value)
				{
				case MPModelPolygonType.Point:
					this.PolygonPointRbt.Checked = true;
					break;
				case MPModelPolygonType.Line:
					this.PolygonLineRbt.Checked = true;
					break;
				case MPModelPolygonType.Fill:
					this.PolygonFillRbt.Checked = true;
					break;
				}
				this.m_ModelPolygon = value;
				this.Update3DModelManagedParallel();
			}
		}

		public MPModelTextureType ModelTexture
		{
			get
			{
				return this.m_ModelTexture;
			}
			set
			{
				switch (value)
				{
				case MPModelTextureType.GrayImage:
					this.TextureGrayRbt.Checked = true;
					break;
				case MPModelTextureType.ColorDepthImage:
					this.TextureColorDepthRbt.Checked = true;
					break;
				case MPModelTextureType.GrayAndColorDepthMixed:
					this.TextureMixedRbt.Checked = true;
					break;
				}
				this.m_ModelTexture = value;
				this.Update3DModelManagedParallel();
			}
		}

		public bool UseNormVector
		{
			get
			{
				return this.m_UseNormVector;
			}
			set
			{
				this.UseNormCkb.Checked = value;
				this.m_UseNormVector = value;
				this.Update3DModelManagedParallel();
			}
		}

		public float ColorDepthImageMin
		{
			get
			{
				return this.m_ColorDepthImageMin;
			}
			set
			{
				if (this.m_ColorDepthImageMin != value)
				{
					this.m_ColorDepthImageMin = value;
					this.InitColorArray();
					this.RangeMinTbx.Text = value.ToString();
					this.Update3DModelManagedParallel();
				}
			}
		}

		public float ColorDepthImageMax
		{
			get
			{
				return this.m_ColorDepthImageMax;
			}
			set
			{
				if (this.m_ColorDepthImageMax != value)
				{
					this.m_ColorDepthImageMax = value;
					this.InitColorArray();
					this.RangeMaxTbx.Text = value.ToString();
					this.Update3DModelManagedParallel();
				}
			}
		}

		public MP3DFrameManaged Data3DManaged //点云显示
		{
			get
			{
				return this.m_Data3DManaged;
			}
			set
			{
				this.m_Data3DManaged = value;
				this.m_DeviceFOV = value.XMax / 0.75f;
				this.m_DeviceDx = this.m_DeviceFOV / (float)(double)value.SensorWidth;
				this.Update3DModelManagedParallel();
			}
		}

		public Bitmap Screenshot
		{
			get
			{
				return this.mGlControl.GrabScreenshot();
			}
		}

		public bool RotateEnable
		{
			get
			{
				return this.RotateEnableCkb.Checked;
			}
			set
			{
				this.RotateEnableCkb.Checked = value;
			}
		}

		public int RotateSpeed
		{
			get
			{
				return this.RotateSpeedScb.Value;
			}
			set
			{
				this.RotateSpeedScb.Value = value;
			}
		}

		public int RotateAmplitude
		{
			get
			{
				return this.RotateAmplitudeScb.Value;
			}
			set
			{
				this.RotateAmplitudeScb.Value = value;
			}
		}

		public float RotatePhase
		{
			get
			{
				return this.m_RotatePhase;
			}
			set
			{
				this.m_RotatePhase = value;
				this.UpdateRotateDisplay();
			}
		}

		public MPDisplayer3D()
		{
			this.m_GrayArr = new Color4[256];
			this.m_ColorArr = new Color4[10001];
			this.InitializeComponent();
			this.InitColorArray();
		}

		private void mGlControl_Load(object sender, EventArgs e)
		{
			try
			{
				GL.ClearColor(Color.Black);
				GL.Enable(EnableCap.DepthTest);
				GL.DepthRange(0f, 5000f);
				GL.DepthFunc(DepthFunction.Less);
				GL.DepthFunc(DepthFunction.Lequal);
				GL.Enable(EnableCap.ColorMaterial);
				GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
				GL.Enable(EnableCap.Light0);
				GL.Enable(EnableCap.Light1);
				GL.Light(LightName.Light0, LightParameter.Position, new float[4]
				{
					50f,
					100f,
					100f,
					1f
				});
				GL.ShadeModel(ShadingModel.Smooth);
				GL.GenBuffers(1, out this.VboID);
				GL.GenBuffers(1, out this.NboID);
				GL.GenBuffers(1, out this.CboID);
				GL.GenBuffers(1, out this.EboID);
				this.SetupViewport();
				switch (this.m_ModelTexture)
				{
				case MPModelTextureType.GrayImage:
					this.TextureGrayRbt.Checked = true;
					break;
				case MPModelTextureType.ColorDepthImage:
					this.TextureColorDepthRbt.Checked = true;
					break;
				case MPModelTextureType.GrayAndColorDepthMixed:
					this.TextureMixedRbt.Checked = true;
					break;
				}
				this.RangeMinTbx.Text = this.m_ColorDepthImageMin.ToString();
				this.RangeMaxTbx.Text = this.m_ColorDepthImageMax.ToString();
				this.m_Loaded = true;
			}
			catch
			{
				MessageBox.Show("3D模型显示错误,请检查是否已正确安装显卡驱动程序(包括集成显卡）");
			}
		}

		private void SetupViewport()
		{
			GL.Viewport(0, 0, this.mGlControl.Width, this.mGlControl.Height);
			double num = (double)this.mGlControl.Width / (double)this.mGlControl.Height;
			Matrix4 matrix = Matrix4.CreatePerspectiveFieldOfView(0.7853982f, (float)num, 1f, 5000f);
			GL.MatrixMode(MatrixMode.Projection);
			GL.LoadMatrix(ref matrix);
		}

		private void InitColorArray()
		{
			for (int i = 0; i < 256; i++)
			{
				this.m_GrayArr[i] = new Color4((byte)i, (byte)i, (byte)i, (byte)i);
				this.m_GrayArr[i].A = 0.5f;
			}
			for (int i = 0; i <= 10000; i++)
			{
				this.m_ColorArr[i] = MP3DFrameManaged.GetDepthColor((float)i / 10000f);
				this.m_ColorArr[i].A = 0.5f;
			}
		}

		private unsafe void Update3DModelManagedParallel()
		{
			DateTime now = DateTime.Now;
			int num = Marshal.SizeOf(typeof(MP3DDataStruct));
			if (!(this.m_Data3DManaged != null && this.m_Data3DManaged.Data != null))
			{
                return;
            }
            
            uint Width = this.m_Data3DManaged.SensorWidth;
            uint sensorHeight = this.m_Data3DManaged.SensorHeight;
            uint num2 = Width * sensorHeight;
            uint[] ElementArrTmp3 = new uint[num2 * 3];
            bool[] ElementValidTmp3 = new bool[num2];
            uint[] ElementArrTmp2 = new uint[num2 * 3];
            bool[] ElementValidTmp2 = new bool[num2];
            float dzTh = this.m_DeviceDx * 5f;
            float SlopTh = 0f - this.m_DeviceDx * 2f;
            if (this.m_Data3DManaged.SensorWidth > 728)
            {
                SlopTh *= 2f;
            }
            int num3 = 0;
            if (!this.m_Loaded)//窗口正常载入
            {
                return;
            }
            #region 窗口正常载入
            if (this.m_IsFirstDisplay)//初始化显示姿态
            {
                this.SetDefaultView();
                this.m_IsFirstDisplay = false;
            }
            if (num2 != this.m_LastSize)//检测内存大小是否发生变化
            {
                this.m_PointCloud = new Vector3[num2];
                this.m_PointColor = new Color4[num2];
                this.m_NormCloud = new Vector3[num2];
                this.m_ElementArray = new uint[num2 * 6];
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.VboID);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(this.m_PointCloud.Length * 3 * 4), this.m_PointCloud, BufferUsageHint.StaticDraw);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.CboID);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(this.m_PointColor.Length * BlittableValueType.StrideOf(this.m_PointColor)), this.m_PointColor, BufferUsageHint.StaticDraw);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.NboID);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(this.m_NormCloud.Length * 3 * 4), this.m_NormCloud, BufferUsageHint.StaticDraw);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.EboID);
                GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(this.m_ElementDisplayCount * 4), this.m_ElementArrayDisplay, BufferUsageHint.StaticDraw);
                this.m_LastSize = num2;
            }
            if (this.m_UseNormVector)
            {
                GL.Enable(EnableCap.Normalize);
                GL.Enable(EnableCap.Lighting);
            }
            else
            {
                GL.Disable(EnableCap.Normalize);
                GL.Disable(EnableCap.Lighting);
            }
            switch (this.m_ModelPolygon)//判断渲染模式
            {
                case MPModelPolygonType.Point:
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Point);
                    break;
                case MPModelPolygonType.Line:
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                    break;
                case MPModelPolygonType.Fill:
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                    break;
            }
            Parallel.For(0L, sensorHeight, delegate (long y)
            {
                for (int j = 0; j < Width; j++)
                {
                    uint num5 = (uint)(j + Width * y);//当前数据点
                    bool flag = this.m_Data3DManaged.Data[num5].Mask == 0;//有效数据点
                    if (flag)
                    {
                        #region 有效数据处理
                        this.m_PointCloud[num5].X = this.m_Data3DManaged.Data[num5].X;
                        this.m_PointCloud[num5].Y = this.m_Data3DManaged.Data[num5].Y;
                        this.m_PointCloud[num5].Z = this.m_Data3DManaged.Data[num5].Z;
                        if (this.m_ModelTexture == MPModelTextureType.GrayImage)
                        {
                            this.m_PointColor[num5] = this.m_GrayArr[this.m_Data3DManaged.Data[num5].Gray];
                        }
                        else if (this.m_ModelTexture == MPModelTextureType.ColorDepthImage)
                        {
                            float num6 = (this.m_Data3DManaged.Data[num5].Z - this.m_ColorDepthImageMin) / (this.m_ColorDepthImageMax - this.m_ColorDepthImageMin);
                            int num7 = (int)(num6 * 10000f);
                            if (num7 > 10000)
                            {
                                num7 = 10000;
                            }
                            else if (num7 < 0)
                            {
                                num7 = 0;
                            }
                            this.m_PointColor[num5] = this.m_ColorArr[num7];
                        }
                        else
                        {
                            float num6 = (this.m_Data3DManaged.Data[num5].Z - this.m_ColorDepthImageMin) / (this.m_ColorDepthImageMax - this.m_ColorDepthImageMin);
                            int num7 = (int)(num6 * 10000f);
                            if (num7 > 10000)
                            {
                                num7 = 10000;
                            }
                            else if (num7 < 0)
                            {
                                num7 = 0;
                            }
                            Color4 color = this.m_ColorArr[num7];
                            Color4 color2 = this.m_GrayArr[this.m_Data3DManaged.Data[num5].Gray];
                            this.m_PointColor[num5].R = color.R / 2f + color2.R / 2f;
                            this.m_PointColor[num5].G = color.G / 2f + color2.G / 2f;
                            this.m_PointColor[num5].B = color.B / 2f + color2.B / 2f;
                        }
                        #endregion
                    }
                    ElementValidTmp3[num5] = false;
                    ElementValidTmp2[num5] = false;
                    if (j > 0 && y > 0) //第二行第二列开始
                    {
                        uint num8 = num5 - Width; //上一行数据点
                        uint num9 = num5 - 1;//前一个数据点
                        uint num10 = num8 - 1;//上一行、前一个数据点
                        bool flag2 = this.m_Data3DManaged.Data[num8].Mask == 0;
                        bool flag3 = this.m_Data3DManaged.Data[num9].Mask == 0;
                        bool flag4 = this.m_Data3DManaged.Data[num10].Mask == 0;
                        uint num11 = num5 * 3;
                        if (flag2 && flag3)//前一个 上一个 是有效点
                        {
                            if (flag)//当前点为有效点
                            {
                                if (this.m_UseNormVector)
                                {
                                    this.CalNormFromPointSafe(out this.m_NormCloud[num5], this.m_Data3DManaged.Data[num5], this.m_Data3DManaged.Data[num8], this.m_Data3DManaged.Data[num9]);
                                }
                                float num12 = this.m_Data3DManaged.Data[num5].X - this.m_Data3DManaged.Data[num9].X;
                                float value = this.m_Data3DManaged.Data[num8].Z - this.m_Data3DManaged.Data[num5].Z;
                                float num13 = this.m_Data3DManaged.Data[num5].Z - this.m_Data3DManaged.Data[num9].Z;
                                if (Math.Abs(value) < dzTh && Math.Abs(num13) < dzTh && num12 > 0f && num13 > SlopTh)
                                {
                                    ElementArrTmp3[num11] = num5;
                                    ElementArrTmp3[num11 + 1] = num9;
                                    ElementArrTmp3[num11 + 2] = num8;
                                    ElementValidTmp3[num5] = true;
                                }
                            }
                            if (flag4)
                            {
                                float num12 = this.m_Data3DManaged.Data[num8].X - this.m_Data3DManaged.Data[num10].X;
                                float value = this.m_Data3DManaged.Data[num8].Z - this.m_Data3DManaged.Data[num10].Z;
                                float num13 = this.m_Data3DManaged.Data[num9].Z - this.m_Data3DManaged.Data[num10].Z;
                                if (Math.Abs(value) < dzTh && Math.Abs(num13) < dzTh && num12 > 0f && value > SlopTh)
                                {
                                    ElementArrTmp2[num11] = num8;
                                    ElementArrTmp2[num11 + 1] = num9;
                                    ElementArrTmp2[num11 + 2] = num10;
                                    ElementValidTmp2[num5] = true;
                                }
                            }
                        }
                    }
                }
            });
            DateTime now2 = DateTime.Now;
            TimeSpan timeSpan = now2 - now;
            now = now2;
            bool[] array = ElementValidTmp3;
            #region 固定数组指针
            fixed (bool* ptr = array)
            {
                array = ElementValidTmp2;
                fixed (bool* ptr4 = array)
                {
                    uint[] array2 = ElementArrTmp3;
                    fixed (uint* ptr3 = array2)
                    {
                        array2 = ElementArrTmp2;
                        fixed (uint* ptr5 = array2)
                        {
                            array2 = this.m_ElementArray;
                            fixed (uint* ptr2 = array2)
                            {
                                for (int i = 0; i < num2; i++)
                                {
                                    int num4 = i * 3;
                                    if (ptr[i])
                                    {
                                        ptr2[num3] = ptr3[num4];
                                        ptr2[num3 + 1] = ptr3[num4 + 1];
                                        ptr2[num3 + 2] = ptr3[num4 + 2];
                                        num3 += 3;
                                    }
                                    if (ptr4[i])
                                    {
                                        ptr2[num3] = ptr5[num4];
                                        ptr2[num3 + 1] = ptr5[num4 + 1];
                                        ptr2[num3 + 2] = ptr5[num4 + 2];
                                        num3 += 3;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion 
            now2 = DateTime.Now;
            timeSpan = now2 - now;
            now = now2;
            this.m_ElementArrayDisplay = this.m_ElementArray;
            this.m_ElementDisplayCount = num3;
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.VboID);
            GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)(this.m_PointCloud.Length * 3 * 4), this.m_PointCloud);
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.CboID);
            GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)(this.m_PointColor.Length * BlittableValueType.StrideOf(this.m_PointColor)), this.m_PointColor);
            if (this.m_UseNormVector)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.NboID);
                GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)(this.m_NormCloud.Length * 3 * 4), this.m_NormCloud);
            }
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.EboID);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(this.m_ElementDisplayCount * 4), this.m_ElementArrayDisplay, BufferUsageHint.StaticDraw);

            now2 = DateTime.Now;
            timeSpan = now2 - now;
            now = now2;
            this.mGlControl_render();
            now2 = DateTime.Now;
            timeSpan = now2 - now;
            now = now2;

            #endregion
        }

        private void mGlControl_Paint(object sender, PaintEventArgs e)
		{
			if (this.m_Loaded)
			{
				this.mGlControl.MakeCurrent();
				this.mGlControl_render();
			}
		}

		private void mGlControl_render()
		{
			if (this.m_Loaded)
			{
				GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
				Matrix4 matrix = Matrix4.LookAt(0f, 0f, 4f * this.m_Zoom, 0f, 0f, 0f, 0f, 1f, 0f);
				GL.MatrixMode(MatrixMode.Modelview);
				GL.LoadMatrix(ref matrix);
				GL.Translate(this.m_XTrans, this.m_YTrans, this.m_zTrans);
				GL.Rotate(this.m_XRot, 1f, 0f, 0f);
				GL.Rotate(this.m_YRot, 0f, 1f, 0f);
				GL.Rotate(this.m_ZRot, 0f, 0f, 1f);
				if (this.m_ElementArrayDisplay != null && this.m_ElementDisplayCount != 0)
				{
					this.Draw3DModel();
				}
				this.mGlControl.SwapBuffers();
			}
		}

		private void DrawCoordinate()
		{
			GL.LineWidth(2f);
			GL.Begin(PrimitiveType.Lines);
			GL.Color3(Color.Red);
			GL.Vertex3(0, 0, 0);
			GL.Vertex3(5, 0, 0);
			GL.Color3(Color.Green);
			GL.Vertex3(0, 0, 0);
			GL.Vertex3(0, 5, 0);
			GL.Color3(Color.Blue);
			GL.Vertex3(0, 0, 0);
			GL.Vertex3(0, 0, 5);
			GL.End();
			GL.Color4(Color.Transparent);
			GL.LineWidth(0f);
		}

		private void Draw3DModel()
		{
			GL.Color3(Color.Silver);
			GL.FrontFace(FrontFaceDirection.Cw);
			GL.EnableClientState(ArrayCap.VertexArray);
			GL.EnableClientState(ArrayCap.ColorArray);
			GL.BindBuffer(BufferTarget.ArrayBuffer, this.VboID);
			GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(0));
			GL.BindBuffer(BufferTarget.ArrayBuffer, this.CboID);
			GL.ColorPointer(4, ColorPointerType.Float, BlittableValueType.StrideOf(this.m_PointColor), new IntPtr(0));
			if (this.m_UseNormVector)
			{
				GL.EnableClientState(ArrayCap.NormalArray);
				GL.BindBuffer(BufferTarget.ArrayBuffer, this.NboID);
				GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(0));
			}
			else
			{
				GL.DisableClientState(ArrayCap.NormalArray);
			}
			GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.EboID);
			GL.DrawElements(PrimitiveType.Triangles, this.m_ElementDisplayCount, DrawElementsType.UnsignedInt, IntPtr.Zero);
		}

		private unsafe void CalNormFromPoint(Vector3* norm, MP3DDataStruct p0, MP3DDataStruct p1, MP3DDataStruct p2)
		{
			float num = p0.X - p1.X;
			float num2 = p0.Y - p1.Y;
			float num3 = p0.Z - p1.Z;
			float num4 = p0.X - p2.X;
			float num5 = p0.Y - p2.Y;
			float num6 = p0.Z - p2.Z;
			norm->X = num2 * num6 - num3 * num5;
			norm->Y = num3 * num4 - num * num6;
			norm->Z = num * num5 - num2 * num4;
		}

		private void CalNormFromPointSafe(out Vector3 norm, MP3DDataStruct p0, MP3DDataStruct p1, MP3DDataStruct p2)
		{
			float num = p0.X - p1.X;
			float num2 = p0.Y - p1.Y;
			float num3 = p0.Z - p1.Z;
			float num4 = p0.X - p2.X;
			float num5 = p0.Y - p2.Y;
			float num6 = p0.Z - p2.Z;
			norm.X = num2 * num6 - num3 * num5;
			norm.Y = num3 * num4 - num * num6;
			norm.Z = num * num5 - num2 * num4;
		}

		private void SetDefaultView()
		{
			this.m_XRot = 0f;
			this.m_YRot = 0f;
			this.m_ZRot = 0f;
			this.m_XTrans = 0f;
			this.m_YTrans = 0f;
			this.m_zTrans = 0f;
			this.m_Zoom = this.m_DeviceFOV * 0.3f;
		}

		private void mGlControl_Resize(object sender, EventArgs e)
		{
			if (this.m_Loaded)
			{
				this.mGlControl.MakeCurrent();
				this.SetupViewport();
				this.SetDefaultView();
				this.mGlControl.Invalidate();
			}
		}

		private void mGlControl_MouseDown(object sender, MouseEventArgs e)
		{
			this.m_LastPos = e.Location;
			if (e.Button == MouseButtons.Middle)
			{
				this.SetDefaultView();
			}
			this.mGlControl.Invalidate();
		}

		private void mGlControl_MouseMove(object sender, MouseEventArgs e)
		{
			Point location = e.Location;
			float num = (float)(location.X - this.m_LastPos.X);
			location = e.Location;
			float num2 = (float)(location.Y - this.m_LastPos.Y);
			if (e.Button == MouseButtons.Left)
			{
				this.rotateBy(num2, num, 0f);
			}
			else if (e.Button == MouseButtons.Right)
			{
				this.translationBy(0.006f * num * this.m_Zoom, -0.006f * num2 * this.m_Zoom, 0f);
			}
			this.m_LastPos = e.Location;
			this.mGlControl.Invalidate();
		}

		private void mGlControl_MouseWheel(object sender, MouseEventArgs e)
		{
			if (e.Delta > 0)
			{
				this.zoomBy((0f - this.m_DeviceFOV) * 0.0125f);
			}
			else
			{
				this.zoomBy(this.m_DeviceFOV * 0.0125f);
			}
			this.mGlControl.Invalidate();
		}

		private void rotateBy(float xAngle, float yAngle, float zAngle)
		{
			this.m_XRot += xAngle;
			this.m_YRot += yAngle;
			this.m_ZRot += zAngle;
		}

		private void translationBy(float x, float y, float z)
		{
			this.m_XTrans += x;
			this.m_YTrans += y;
			this.m_zTrans += z;
		}

		private void zoomBy(float z)
		{
			this.m_Zoom += z;
		}

		private void SettingsExpandBtn_Click(object sender, EventArgs e)
		{
			this.Display3DSettingsGbp.Visible = !this.Display3DSettingsGbp.Visible;
			base.Visible = !base.Visible;
			base.Visible = !base.Visible;
		}

		private void TextureGrayRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.TextureGrayRbt.Checked)
			{
				this.ModelTexture = MPModelTextureType.GrayImage;
			}
		}

		private void TextureColorDepthRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.TextureColorDepthRbt.Checked)
			{
				this.ModelTexture = MPModelTextureType.ColorDepthImage;
			}
		}

		private void TextureMixedRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.TextureMixedRbt.Checked)
			{
				this.ModelTexture = MPModelTextureType.GrayAndColorDepthMixed;
			}
		}

		private void SetRangeBtn_Click(object sender, EventArgs e)
		{
			float num;
			float num2;
			try
			{
				num = float.Parse(this.RangeMinTbx.Text);
				num2 = float.Parse(this.RangeMaxTbx.Text);
			}
			catch
			{
				MessageBox.Show("Invalid inpu, shoud be float and min < max.");
				return;
			}
			if (num >= num2)
			{
				MessageBox.Show("Invalid inpu, shoud be float and min < max.");
			}
			else
			{
				this.ColorDepthImageMin = num;
				this.ColorDepthImageMax = num2;
			}
		}

		private void UseNormCkb_CheckedChanged(object sender, EventArgs e)
		{
			this.UseNormVector = this.UseNormCkb.Checked;
		}

		private void PolygonPointRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.PolygonPointRbt.Checked)
			{
				this.ModelPolygon = MPModelPolygonType.Point;
			}
		}

		private void PolygonLineRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.PolygonLineRbt.Checked)
			{
				this.ModelPolygon = MPModelPolygonType.Line;
			}
		}

		private void PolygonFillRbt_CheckedChanged(object sender, EventArgs e)
		{
			if (this.PolygonFillRbt.Checked)
			{
				this.ModelPolygon = MPModelPolygonType.Fill;
			}
		}

		private void RotateEnableCkb_CheckedChanged(object sender, EventArgs e)
		{
			this.RotateTmr.Enabled = this.RotateEnableCkb.Checked;
		}

		private void RotateTmr_Tick(object sender, EventArgs e)
		{
			if (base.Visible)
			{
				this.m_RotateCnt += this.RotateSpeedScb.Value;
				if (this.m_RotateCnt > 500)
				{
					this.m_RotateCnt = 0;
				}
				this.m_RotatePhase = (float)((double)((float)this.m_RotateCnt / 500f * (float)this.RotateSpeedScb.Value * 2f) * 3.1415926535897931);
				this.UpdateRotateDisplay();
			}
		}

		private void UpdateRotateDisplay()
		{
			this.tmpx = (float)Math.Sin((double)this.m_RotatePhase);
			this.tmpy = (float)Math.Cos((double)this.m_RotatePhase);
			this.rotateBy((float)(this.RotateAmplitudeScb.Value * 3) * (this.tmpx - this.dx), (float)(this.RotateAmplitudeScb.Value * 3) * (this.tmpy - this.dy), 0f);
			this.dx = this.tmpx;
			this.dy = this.tmpy;
			this.mGlControl.Invalidate();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			this.mGlControl = new GLControl();
			this.SettingsExpandBtn = new Button();
			this.Display3DSettingsGbp = new GroupBox();
			this.RotationGbp = new GroupBox();
			this.RotateAmplitudeScb = new HScrollBar();
			this.RotateSpeedScb = new HScrollBar();
			this.label5 = new Label();
			this.label1 = new Label();
			this.RotateEnableCkb = new CheckBox();
			this.groupBox2 = new GroupBox();
			this.PolygonFillRbt = new RadioButton();
			this.PolygonPointRbt = new RadioButton();
			this.PolygonLineRbt = new RadioButton();
			this.groupBox1 = new GroupBox();
			this.TextureMixedRbt = new RadioButton();
			this.TextureGrayRbt = new RadioButton();
			this.TextureColorDepthRbt = new RadioButton();
			this.UseNormCkb = new CheckBox();
			this.SetRangeBtn = new Button();
			this.label4 = new Label();
			this.RangeMaxTbx = new TextBox();
			this.label3 = new Label();
			this.RangeMinTbx = new TextBox();
			this.label2 = new Label();
			this.RotateTmr = new Timer(this.components);
			this.Display3DSettingsGbp.SuspendLayout();
			this.RotationGbp.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			base.SuspendLayout();
			this.mGlControl.BackColor = Color.Black;
			this.mGlControl.Dock = DockStyle.Fill;
			this.mGlControl.Location = new Point(0, 0);
			this.mGlControl.Margin = new Padding(3, 6, 3, 6);
			this.mGlControl.Name = "mGlControl";
			this.mGlControl.Size = new Size(386, 415);
			this.mGlControl.TabIndex = 0;
			this.mGlControl.VSync = false;
			this.mGlControl.Load += this.mGlControl_Load;
			this.mGlControl.Paint += this.mGlControl_Paint;
			this.mGlControl.MouseDown += this.mGlControl_MouseDown;
			this.mGlControl.MouseMove += this.mGlControl_MouseMove;
			this.mGlControl.MouseWheel += this.mGlControl_MouseWheel;
			this.mGlControl.Resize += this.mGlControl_Resize;
			this.SettingsExpandBtn.Location = new Point(3, 4);
			this.SettingsExpandBtn.Margin = new Padding(3, 4, 3, 4);
			this.SettingsExpandBtn.Name = "SettingsExpandBtn";
			this.SettingsExpandBtn.Size = new Size(168, 29);
			this.SettingsExpandBtn.TabIndex = 1;
			this.SettingsExpandBtn.Text = "Expand / Collapse Setting";
			this.SettingsExpandBtn.UseVisualStyleBackColor = true;
			this.SettingsExpandBtn.Click += this.SettingsExpandBtn_Click;
			this.Display3DSettingsGbp.BackColor = Color.Black;
			this.Display3DSettingsGbp.Controls.Add(this.RotationGbp);
			this.Display3DSettingsGbp.Controls.Add(this.groupBox2);
			this.Display3DSettingsGbp.Controls.Add(this.groupBox1);
			this.Display3DSettingsGbp.Controls.Add(this.UseNormCkb);
			this.Display3DSettingsGbp.Controls.Add(this.SetRangeBtn);
			this.Display3DSettingsGbp.Controls.Add(this.label4);
			this.Display3DSettingsGbp.Controls.Add(this.RangeMaxTbx);
			this.Display3DSettingsGbp.Controls.Add(this.label3);
			this.Display3DSettingsGbp.Controls.Add(this.RangeMinTbx);
			this.Display3DSettingsGbp.Controls.Add(this.label2);
			this.Display3DSettingsGbp.ForeColor = Color.White;
			this.Display3DSettingsGbp.Location = new Point(3, 41);
			this.Display3DSettingsGbp.Margin = new Padding(3, 4, 3, 4);
			this.Display3DSettingsGbp.Name = "Display3DSettingsGbp";
			this.Display3DSettingsGbp.Padding = new Padding(3, 4, 3, 4);
			this.Display3DSettingsGbp.Size = new Size(168, 370);
			this.Display3DSettingsGbp.TabIndex = 2;
			this.Display3DSettingsGbp.TabStop = false;
			this.Display3DSettingsGbp.Text = "Display Settings";
			this.Display3DSettingsGbp.Visible = false;
			this.RotationGbp.Controls.Add(this.RotateAmplitudeScb);
			this.RotationGbp.Controls.Add(this.RotateSpeedScb);
			this.RotationGbp.Controls.Add(this.label5);
			this.RotationGbp.Controls.Add(this.label1);
			this.RotationGbp.Controls.Add(this.RotateEnableCkb);
			this.RotationGbp.Location = new Point(6, 271);
			this.RotationGbp.Name = "RotationGbp";
			this.RotationGbp.Size = new Size(156, 92);
			this.RotationGbp.TabIndex = 17;
			this.RotationGbp.TabStop = false;
			this.RotationGbp.Text = "Rotation";
			this.RotateAmplitudeScb.LargeChange = 1;
			this.RotateAmplitudeScb.Location = new Point(50, 70);
			this.RotateAmplitudeScb.Maximum = 5;
			this.RotateAmplitudeScb.Minimum = 1;
			this.RotateAmplitudeScb.Name = "RotateAmplitudeScb";
			this.RotateAmplitudeScb.Size = new Size(90, 15);
			this.RotateAmplitudeScb.TabIndex = 4;
			this.RotateAmplitudeScb.Value = 3;
			this.RotateSpeedScb.LargeChange = 1;
			this.RotateSpeedScb.Location = new Point(50, 48);
			this.RotateSpeedScb.Maximum = 5;
			this.RotateSpeedScb.Minimum = 1;
			this.RotateSpeedScb.Name = "RotateSpeedScb";
			this.RotateSpeedScb.Size = new Size(90, 15);
			this.RotateSpeedScb.TabIndex = 3;
			this.RotateSpeedScb.Value = 2;
			this.label5.AutoSize = true;
			this.label5.Location = new Point(6, 68);
			this.label5.Name = "label5";
			this.label5.Size = new Size(35, 17);
			this.label5.TabIndex = 2;
			this.label5.Text = "Amp";
			this.label1.AutoSize = true;
			this.label1.Location = new Point(6, 46);
			this.label1.Name = "label1";
			this.label1.Size = new Size(31, 17);
			this.label1.TabIndex = 1;
			this.label1.Text = "Spd";
			this.RotateEnableCkb.AutoSize = true;
			this.RotateEnableCkb.Location = new Point(9, 22);
			this.RotateEnableCkb.Name = "RotateEnableCkb";
			this.RotateEnableCkb.Size = new Size(107, 21);
			this.RotateEnableCkb.TabIndex = 0;
			this.RotateEnableCkb.Text = "Rotate Model";
			this.RotateEnableCkb.UseVisualStyleBackColor = true;
			this.RotateEnableCkb.CheckedChanged += this.RotateEnableCkb_CheckedChanged;
			this.groupBox2.Controls.Add(this.PolygonFillRbt);
			this.groupBox2.Controls.Add(this.PolygonPointRbt);
			this.groupBox2.Controls.Add(this.PolygonLineRbt);
			this.groupBox2.ForeColor = Color.White;
			this.groupBox2.Location = new Point(87, 23);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new Size(75, 108);
			this.groupBox2.TabIndex = 16;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Polygon";
			this.PolygonFillRbt.AutoSize = true;
			this.PolygonFillRbt.Checked = true;
			this.PolygonFillRbt.Location = new Point(6, 80);
			this.PolygonFillRbt.Margin = new Padding(3, 4, 3, 4);
			this.PolygonFillRbt.Name = "PolygonFillRbt";
			this.PolygonFillRbt.Size = new Size(41, 21);
			this.PolygonFillRbt.TabIndex = 3;
			this.PolygonFillRbt.TabStop = true;
			this.PolygonFillRbt.Text = "Fill";
			this.PolygonFillRbt.UseVisualStyleBackColor = true;
			this.PolygonFillRbt.CheckedChanged += this.PolygonFillRbt_CheckedChanged;
			this.PolygonPointRbt.AutoSize = true;
			this.PolygonPointRbt.Location = new Point(6, 22);
			this.PolygonPointRbt.Margin = new Padding(3, 4, 3, 4);
			this.PolygonPointRbt.Name = "PolygonPointRbt";
			this.PolygonPointRbt.Size = new Size(55, 21);
			this.PolygonPointRbt.TabIndex = 1;
			this.PolygonPointRbt.Text = "Point";
			this.PolygonPointRbt.UseVisualStyleBackColor = true;
			this.PolygonPointRbt.CheckedChanged += this.PolygonPointRbt_CheckedChanged;
			this.PolygonLineRbt.AutoSize = true;
			this.PolygonLineRbt.Location = new Point(6, 51);
			this.PolygonLineRbt.Margin = new Padding(3, 4, 3, 4);
			this.PolygonLineRbt.Name = "PolygonLineRbt";
			this.PolygonLineRbt.Size = new Size(49, 21);
			this.PolygonLineRbt.TabIndex = 2;
			this.PolygonLineRbt.Text = "Line";
			this.PolygonLineRbt.UseVisualStyleBackColor = true;
			this.PolygonLineRbt.CheckedChanged += this.PolygonLineRbt_CheckedChanged;
			this.groupBox1.Controls.Add(this.TextureMixedRbt);
			this.groupBox1.Controls.Add(this.TextureGrayRbt);
			this.groupBox1.Controls.Add(this.TextureColorDepthRbt);
			this.groupBox1.ForeColor = Color.White;
			this.groupBox1.Location = new Point(6, 23);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(75, 108);
			this.groupBox1.TabIndex = 15;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Texture";
			this.TextureMixedRbt.AutoSize = true;
			this.TextureMixedRbt.Checked = true;
			this.TextureMixedRbt.Location = new Point(6, 80);
			this.TextureMixedRbt.Margin = new Padding(3, 4, 3, 4);
			this.TextureMixedRbt.Name = "TextureMixedRbt";
			this.TextureMixedRbt.Size = new Size(62, 21);
			this.TextureMixedRbt.TabIndex = 3;
			this.TextureMixedRbt.TabStop = true;
			this.TextureMixedRbt.Text = "Mixed";
			this.TextureMixedRbt.UseVisualStyleBackColor = true;
			this.TextureMixedRbt.CheckedChanged += this.TextureMixedRbt_CheckedChanged;
			this.TextureGrayRbt.AutoSize = true;
			this.TextureGrayRbt.Location = new Point(6, 22);
			this.TextureGrayRbt.Margin = new Padding(3, 4, 3, 4);
			this.TextureGrayRbt.Name = "TextureGrayRbt";
			this.TextureGrayRbt.Size = new Size(53, 21);
			this.TextureGrayRbt.TabIndex = 1;
			this.TextureGrayRbt.Text = "Gray";
			this.TextureGrayRbt.UseVisualStyleBackColor = true;
			this.TextureGrayRbt.CheckedChanged += this.TextureGrayRbt_CheckedChanged;
			this.TextureColorDepthRbt.AutoSize = true;
			this.TextureColorDepthRbt.Location = new Point(6, 51);
			this.TextureColorDepthRbt.Margin = new Padding(3, 4, 3, 4);
			this.TextureColorDepthRbt.Name = "TextureColorDepthRbt";
			this.TextureColorDepthRbt.Size = new Size(64, 21);
			this.TextureColorDepthRbt.TabIndex = 2;
			this.TextureColorDepthRbt.Text = "Height";
			this.TextureColorDepthRbt.UseVisualStyleBackColor = true;
			this.TextureColorDepthRbt.CheckedChanged += this.TextureColorDepthRbt_CheckedChanged;
			this.UseNormCkb.AutoSize = true;
			this.UseNormCkb.Checked = true;
			this.UseNormCkb.CheckState = CheckState.Checked;
			this.UseNormCkb.Location = new Point(10, 244);
			this.UseNormCkb.Name = "UseNormCkb";
			this.UseNormCkb.Size = new Size(119, 21);
			this.UseNormCkb.TabIndex = 14;
			this.UseNormCkb.Text = "Use Norm Vect.";
			this.UseNormCkb.UseVisualStyleBackColor = true;
			this.UseNormCkb.CheckedChanged += this.UseNormCkb_CheckedChanged;
			this.SetRangeBtn.BackColor = SystemColors.Control;
			this.SetRangeBtn.ForeColor = SystemColors.ControlText;
			this.SetRangeBtn.Location = new Point(113, 170);
			this.SetRangeBtn.Margin = new Padding(3, 4, 3, 4);
			this.SetRangeBtn.Name = "SetRangeBtn";
			this.SetRangeBtn.Size = new Size(38, 57);
			this.SetRangeBtn.TabIndex = 3;
			this.SetRangeBtn.Text = "Set";
			this.SetRangeBtn.UseVisualStyleBackColor = false;
			this.SetRangeBtn.Click += this.SetRangeBtn_Click;
			this.label4.AutoSize = true;
			this.label4.Location = new Point(5, 208);
			this.label4.Name = "label4";
			this.label4.Size = new Size(36, 17);
			this.label4.TabIndex = 8;
			this.label4.Text = "Max:";
			this.RangeMaxTbx.Location = new Point(40, 204);
			this.RangeMaxTbx.Margin = new Padding(3, 4, 3, 4);
			this.RangeMaxTbx.Name = "RangeMaxTbx";
			this.RangeMaxTbx.Size = new Size(58, 23);
			this.RangeMaxTbx.TabIndex = 7;
			this.label3.AutoSize = true;
			this.label3.Location = new Point(5, 173);
			this.label3.Name = "label3";
			this.label3.Size = new Size(33, 17);
			this.label3.TabIndex = 6;
			this.label3.Text = "Min:";
			this.RangeMinTbx.Location = new Point(40, 169);
			this.RangeMinTbx.Margin = new Padding(3, 4, 3, 4);
			this.RangeMinTbx.Name = "RangeMinTbx";
			this.RangeMinTbx.Size = new Size(58, 23);
			this.RangeMinTbx.TabIndex = 5;
			this.label2.AutoSize = true;
			this.label2.Font = new Font("微软雅黑", 9f, FontStyle.Bold, GraphicsUnit.Point, 134);
			this.label2.Location = new Point(6, 145);
			this.label2.Name = "label2";
			this.label2.Size = new Size(129, 17);
			this.label2.TabIndex = 4;
			this.label2.Text = "Height Range(mm):";
			this.RotateTmr.Interval = 50;
			this.RotateTmr.Tick += this.RotateTmr_Tick;
			base.AutoScaleDimensions = new SizeF(7f, 17f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.Controls.Add(this.Display3DSettingsGbp);
			base.Controls.Add(this.SettingsExpandBtn);
			base.Controls.Add(this.mGlControl);
			this.Font = new Font("微软雅黑", 9f, FontStyle.Regular, GraphicsUnit.Point, 134);
			base.Margin = new Padding(3, 4, 3, 4);
			base.Name = "MPDisplayer3D";
			base.Size = new Size(386, 415);
			this.Display3DSettingsGbp.ResumeLayout(false);
			this.Display3DSettingsGbp.PerformLayout();
			this.RotationGbp.ResumeLayout(false);
			this.RotationGbp.PerformLayout();
			this.groupBox2.ResumeLayout(false);
			this.groupBox2.PerformLayout();
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			base.ResumeLayout(false);
		}
	}
}
